﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>js随即切换图片</title>
    <script type="text/javascript">
        //代码版本一
        /*
        var i = 1;
        function changeImage() {
        if (i >= 6) {
        i = 1;
        }
        document.getElementById("adimage").src = "images/ad_0" + i + ".jpg";
        i++;
        }
        window.onload = function () {
        setInterval(changeImage, 1000);
        }
        */

        //代码版本二

        function changeImage() {
            var randomBgIndex = Math.round(Math.random() * 4 + 1);
            document.getElementById("adimage").src = "images/ad_0" + randomBgIndex + ".jpg";
        }
        window.onload = function () {
            setInterval(changeImage, 1000);
        }
        
    </script>
</head>
<body>
    <div>
        <img src="images/ad_01.jpg" alt="" id="adimage" />
    </div>
</body>
</html>
